﻿using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using TianYu.Core.Common;

namespace TianYu.Core.DataBase.Repository
{
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : BaseModel, new()
    {
        readonly IServiceProvider _serviceProvider;
        private readonly IUnitOfWork _unitOfWork;
        private readonly SqlSugarScope _sqlSugarClient;
        private readonly Guid _contextID;
        public SqlSugarScope Db
        {
            get { return _sqlSugarClient; }
        }

        public BaseRepository(IUnitOfWork unitOfWork, IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _unitOfWork = unitOfWork;
            _sqlSugarClient = _unitOfWork.GetDbClient();
            //勿删
            _contextID = _sqlSugarClient.ContextID;
        }



        #region 增

        public void AddQueue(string sql, List<SugarParameter> parsmeters) => _sqlSugarClient.AddQueue(sql, parsmeters);

        public void AddQueue(string sql, SugarParameter parsmeter) => _sqlSugarClient.AddQueue(sql, parsmeter);




        /// <summary>写入实体数据</summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public async Task<int> AddAsync(TEntity entity)
        {
            var insert = _sqlSugarClient.Insertable(entity);
            //这里你可以返回TEntity，这样的话就可以获取id值，无论主键是什么类型
            //var return3 = await insert.ExecuteReturnEntityAsync();
            return await insert.ExecuteReturnIdentityAsync();
        }

        /// <summary>写入实体数据</summary>
        /// <param name="entity">实体类</param>
        /// <param name="insertColumns">指定只插入列</param>
        /// <returns>返回自增量列</returns>
        public async Task<int> AddAsync(TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
        {
            var insert = _sqlSugarClient.Insertable(entity);
            if (insertColumns == null)
            {
                return await insert.ExecuteReturnIdentityAsync();
            }
            else
            {
                return await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync();
            }
        }

        /// <summary>批量插入实体(速度快)</summary>
        /// <param name="listEntity">实体集合</param>
        /// <returns>影响行数</returns>
        public async Task<int> AddAsync(List<TEntity> listEntity)
            => await _sqlSugarClient.Insertable(listEntity.ToArray()).ExecuteCommandAsync();

        /// <summary>写入实体数据</summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public void AddQueue(TEntity entity) => _sqlSugarClient.Insertable(entity).AddQueue();

        /// <summary>写入实体数据</summary>
        /// <param name="entity">实体类</param>
        /// <param name="insertColumns">指定只插入列</param>
        /// <returns>返回自增量列</returns>
        public void AddQueue(TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
        {
            var insert = _sqlSugarClient.Insertable(entity);
            if (insertColumns == null)
            {
                insert.AddQueue();
            }
            else
            {
                insert.InsertColumns(insertColumns).AddQueue();
            }
        }

        /// <summary>批量插入实体(速度快)</summary>
        /// <param name="listEntity">实体集合</param>
        /// <returns>影响行数</returns>
        public void AddQueue(List<TEntity> listEntity) => _sqlSugarClient.Insertable(listEntity.ToArray()).AddQueue();

        #endregion


        #region 删
        /// <summary>根据实体删除一条数据</summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(TEntity entity)
        => await _sqlSugarClient.Deleteable(entity).ExecuteCommandHasChangeAsync();

        /// <summary>删除指定ID的数据</summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdAsync(object id)
        => await _sqlSugarClient.Deleteable<TEntity>(id).ExecuteCommandHasChangeAsync();

        /// <summary>删除指定ID集合的数据(批量删除)</summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdsAsync(params object[] ids)
        => await _sqlSugarClient.Deleteable<TEntity>().In(ids).ExecuteCommandHasChangeAsync();


        /// <summary>删除指定ID集合的数据</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Deleteable<TEntity>().Where(whereExpression).ExecuteCommandHasChangeAsync();

        /// <summary>根据实体删除一条数据</summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public void DeleteQueue(TEntity entity)
        => _sqlSugarClient.Deleteable(entity).AddQueue();

        /// <summary>删除指定ID的数据</summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public void DeleteQueue(object id)
        => _sqlSugarClient.Deleteable<TEntity>(id).AddQueue();

        /// <summary>删除指定ID集合的数据(批量删除)</summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        public void DeleteQueue(params object[] ids)
        => _sqlSugarClient.Deleteable<TEntity>().In(ids).AddQueue();

        /// <summary>删除指定ID集合的数据（指令进行缓存不立即执行，调用<c>SaveQueuesAsync(bool)</c>批量提交）</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns></returns>
        public void DeleteQueue(Expression<Func<TEntity, bool>> whereExpression)
        => _sqlSugarClient.Deleteable<TEntity>().Where(whereExpression).AddQueue();
        #endregion


        #region 改


        /// <summary>更新实体数据(主键为条件)</summary>
        /// <param name="entity">博文实体类</param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(TEntity entity)
            => await _sqlSugarClient.Updateable(entity).ExecuteCommandHasChangeAsync();

        public async Task<bool> UpdateAsync(TEntity entity, string strWhere)
            => await _sqlSugarClient.Updateable(entity).Where(strWhere).ExecuteCommandHasChangeAsync();

        public async Task<bool> UpdateAsync(string strSql, params SugarParameter[] parameters)
            => await _sqlSugarClient.Ado.ExecuteCommandAsync(strSql, parameters) > 0;

        public async Task<bool> UpdateAsync(object operateAnonymousObjects)
            => await _sqlSugarClient.Updateable<TEntity>(operateAnonymousObjects).ExecuteCommandAsync() > 0;

        public async Task<bool> UpdateAsync(
          TEntity entity,
          List<string> lstColumns = null,
          List<string> lstIgnoreColumns = null,
          string strWhere = "")
        {
            IUpdateable<TEntity> up = _sqlSugarClient.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return await up.ExecuteCommandHasChangeAsync();
        }

        public async Task<bool> UpdateAsync(
         TEntity entity,
         Expression<Func<TEntity, object>> columnsExpression = null,
         Expression<Func<TEntity, object>> ignoreColumnsExpression = null,
         Expression<Func<TEntity, bool>> whereExpression = null)
        {
            IUpdateable<TEntity> up = _sqlSugarClient.Updateable(entity);
            if (ignoreColumnsExpression != null)
            {
                up = up.IgnoreColumns(ignoreColumnsExpression);
            }
            if (columnsExpression != null)
            {
                up = up.UpdateColumns(columnsExpression);
            }
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            return await up.ExecuteCommandHasChangeAsync();
        }

        public async Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression)
         => await Db.Updateable<TEntity>().SetColumns(columns).Where(whereExpression).ExecuteCommandAsync() > 0;

        /// <summary>更新实体数据(主键为条件)</summary>
        /// <param name="entity">博文实体类</param>
        /// <returns></returns>
        public void UpdateQueue(TEntity entity)
            => _sqlSugarClient.Updateable(entity).AddQueue();

        public void UpdateQueue(TEntity entity, string strWhere)
            => _sqlSugarClient.Updateable(entity).Where(strWhere).AddQueue();

        public void UpdateQueue(object operateAnonymousObjects)
            => _sqlSugarClient.Updateable<TEntity>(operateAnonymousObjects).AddQueue();

        public void UpdateQueue(
          TEntity entity,
          List<string> lstColumns = null,
          List<string> lstIgnoreColumns = null,
          string strWhere = "")
        {
            IUpdateable<TEntity> up = _sqlSugarClient.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            up.AddQueue();
        }

        public void UpdateQueue(
         TEntity entity,
         Expression<Func<TEntity, object>> columnsExpression = null,
         Expression<Func<TEntity, object>> ignoreColumnsExpression = null,
         Expression<Func<TEntity, bool>> whereExpression = null)
        {
            IUpdateable<TEntity> up = _sqlSugarClient.Updateable(entity);
            if (ignoreColumnsExpression != null)
            {
                up = up.IgnoreColumns(ignoreColumnsExpression);
            }
            if (columnsExpression != null)
            {
                up = up.UpdateColumns(columnsExpression);
            }
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            up.AddQueue();
        }

        public void UpdateQueue(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression)
        => Db.Updateable<TEntity>().SetColumns(columns).Where(whereExpression).AddQueue();

        public async Task<bool> UpdateWithChangedAsync(TEntity entity, Expression<Func<TEntity, bool>> whereExpression = null, Expression<Func<TEntity, object>> ignoreColumnsExpression = null)
        {
            var updateColumns = entity.ChangedPropertys.ToArray();
            Common.Check.NotEmpty(updateColumns, nameof(updateColumns));

            var up = _sqlSugarClient.Updateable(entity).UpdateColumns(updateColumns);
            if (ignoreColumnsExpression != null)
            {
                up = up.IgnoreColumns(ignoreColumnsExpression);
            }
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            return await up.ExecuteCommandHasChangeAsync();
        }

        public void UpdateWithChangedQueue(TEntity entity, Expression<Func<TEntity, bool>> whereExpression = null, Expression<Func<TEntity, object>> ignoreColumnsExpression = null)
        {
            var updateColumns = entity.ChangedPropertys.ToArray();
            Common.Check.NotEmpty(updateColumns, nameof(updateColumns));

            var up = _sqlSugarClient.Updateable(entity).UpdateColumns(updateColumns);
            if (ignoreColumnsExpression != null)
            {
                up = up.IgnoreColumns(ignoreColumnsExpression);
            }
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            up.AddQueue();
        }

        #endregion


        #region 查

        public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression)
       => await _sqlSugarClient.Queryable<TEntity>().AnyAsync(expression);

        public async Task<TResult> MaxAsync<TResult>(Expression<Func<TEntity, TResult>> columnExpression)
        => await _sqlSugarClient.Queryable<TEntity>().MaxAsync<TResult>(columnExpression);

        public async Task<TResult> MaxAsync<TResult>(Expression<Func<TEntity, TResult>> columnExpression, Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).MaxAsync<TResult>(columnExpression);

        public async Task<TResult> SumAsync<TResult>(Expression<Func<TEntity, TResult>> columnExpression)
        => await _sqlSugarClient.Queryable<TEntity>().SumAsync<TResult>(columnExpression);

        public async Task<TResult> SumAsync<TResult>(Expression<Func<TEntity, TResult>> columnExpression, Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).SumAsync<TResult>(columnExpression);

        /// <summary>根据ID查询一条数据</summary>
        /// <param name="objId">id（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <param name="blnUseCache">是否使用缓存</param>
        /// <returns>数据实体</returns>
        public async Task<TEntity> GetAsync(object objId)
        => await _sqlSugarClient.Queryable<TEntity>().In(objId).SingleAsync();

        /// <summary>根据ID查询一条数据</summary>
        /// <param name="objId">id（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <param name="blnUseCache">是否使用缓存</param>
        /// <returns>数据实体</returns>
        public async Task<TEntity> GetAsync(object objId, bool blnUseCache = false)
        => await _sqlSugarClient.Queryable<TEntity>().WithCacheIF(blnUseCache).In(objId).SingleAsync();

        /// <summary>根据条件查询一条数据</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>数据实体</returns>
        public async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).SingleAsync();

        /// <summary>根据条件查询一条数据</summary>
        /// <param name="columnExpression">返回列表达式</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>数据实体</returns>
        public async Task<TEntity> GetAsync(Expression<Func<TEntity, TEntity>> columnExpression, Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).Select(columnExpression).SingleAsync();

        /// <summary>根据ID查询数据</summary>
        /// <param name="lstIds">id列表（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <returns>数据实体列表</returns>
        public async Task<List<TEntity>> GetListAsync(params object[] lstIds)
        => await _sqlSugarClient.Queryable<TEntity>().In(lstIds).ToListAsync();

        /// <summary>根据条件查询一条数据(多条根据排序取第一条记录)</summary> 
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderByExpression">排序表达式</param>
        /// <returns>数据实体</returns>
        public async Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).OrderBy(orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).FirstAsync();

        /// <summary>根据条件查询一条数据(多条根据排序取第一条记录)</summary> 
        /// <param name="whereExpression">条件表达式</param>
        public async Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).FirstAsync();

        /// <summary>查询指定字段的值（返回一个值）</summary>  
        public async Task<TResult> GetFieldValueAsync<TResult>(Expression<Func<TEntity, TResult>> columnExpression, Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).Select(columnExpression).FirstAsync();

        /// <summary>查询指定字段的值（返回多个值）</summary>  
        public async Task<List<TResult>> QueryFieldValueAsync<TResult>(Expression<Func<TEntity, TResult>> columnExpression, Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).Select(columnExpression).ToListAsync();

        /// <summary>查询数据条数</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns></returns>
        public async Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().CountAsync(whereExpression);

        /// <summary>查询所有数据</summary>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync() => await _sqlSugarClient.Queryable<TEntity>().ToListAsync();

        /// <summary>查询数据列表</summary>
        /// <param name="strWhere">条件</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string strWhere)
        => await _sqlSugarClient.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();

        /// <summary>查询数据列表</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression)
        => await _sqlSugarClient.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToListAsync();

        /// <summary>按照特定列查询数据列表</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression)
        => await _sqlSugarClient.Queryable<TEntity>().Select(expression).ToListAsync();

        /// <summary>按照特定列查询数据列表带条件排序</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="whereExpression">过滤条件</param>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        => await _sqlSugarClient.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync();

        /// <summary>查询一个列表</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        => await _sqlSugarClient.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToListAsync();

        /// <summary>查询一个列表</summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        => await _sqlSugarClient.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync();

        /// <summary>查询一个列表</summary>
        /// <param name="strWhere">条件</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string strWhere, string strOrderByFileds)
        => await _sqlSugarClient.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();

        /// <summary>查询前N条数据</summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <param name="intTop">前N条</param>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true, int intTop = 10)
            => await _sqlSugarClient.Queryable<TEntity>()
            .OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc)
            .WhereIF(whereExpression != null, whereExpression).Take(intTop).ToListAsync();

        /// <summary>查询前N条数据</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(
            Expression<Func<TEntity, bool>> whereExpression,
            int intTop,
            string strOrderByFileds)
            => await _sqlSugarClient.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToListAsync();

        /// <summary>查询前N条数据</summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(
            string strWhere,
            int intTop,
            string strOrderByFileds)
            => await _sqlSugarClient.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToListAsync();

        /// <summary>根据sql语句查询</summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>泛型集合</returns>
        public async Task<List<T>> QuerySqlAsync<T>(string strSql, params SugarParameter[] parameters) where T : new()
        => await _sqlSugarClient.Ado.SqlQueryAsync<T>(strSql, parameters);

        /// <summary>根据sql语句查询</summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataTable</returns>
        public async Task<DataTable> QueryTableAsync(string strSql, params SugarParameter[] parameters)
        => await _sqlSugarClient.Ado.GetDataTableAsync(strSql, parameters);

        /// <summary>根据ID查询数据</summary>
        /// <param name="ids">id列表（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <returns>返回Dictionary，Key为IsPrimaryKey=true的属性值,Value为TEntity</returns>
        public async Task<Dictionary<object, TEntity>> QueryDictionaryAsync(params object[] ids)
        {
            var list = await GetListAsync(ids);
            var keyProperty = this.Db.GetEntityPrimaryKeyProperty<TEntity>();
            return list.ToDictionary(a => keyProperty.PropertyInfo.GetValue(a), a => a);
        }

        /// <summary>分页查询</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(
            Expression<Func<TEntity, bool>> whereExpression,
            int intPageIndex,
            int intPageSize,
            string strOrderByFileds)
            => await _sqlSugarClient.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize);

        /// <summary>分页查询</summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(
          string strWhere,
          int intPageIndex,
          int intPageSize,
          string strOrderByFileds)
            => await _sqlSugarClient.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageListAsync(intPageIndex, intPageSize);

        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, TEntity>> columnExpression, Expression<Func<TEntity, bool>> whereExpression)
            => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).Select(columnExpression).ToListAsync();

        public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> columnExpression, Expression<Func<TEntity, bool>> whereExpression)
            => await _sqlSugarClient.Queryable<TEntity>().Where(whereExpression).Select(columnExpression).ToListAsync();

        /// <summary>分页查询[使用版本，其他分页未测试]</summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            var list = await _sqlSugarClient.Queryable<TEntity>()
             .WhereIF(whereExpression != null, whereExpression)
             .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
             .ToPageListAsync(intPageIndex, intPageSize, totalCount);

            return new PageModel<TEntity>() { dataCount = totalCount, page = intPageIndex, pageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 根据表达式，排序字段，分页查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression">排序表达式</param>
        /// <param name="isAsc">是否升序</param>
        /// <param name="intPageIndex"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        public async Task<PageModel<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, Expression<Func<TEntity, object>> orderByExpression = null, bool isAsc = true)
        {
            RefAsync<int> totalCount = 0;
            var list = await _sqlSugarClient.Queryable<TEntity>()
             .WhereIF(whereExpression != null, whereExpression)
             .OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc)
             .ToPageListAsync(intPageIndex, intPageSize, totalCount);

            return new PageModel<TEntity>() { dataCount = totalCount, page = intPageIndex, pageSize = intPageSize, data = list };
        }

        #endregion


        public async Task<int> SaveQueuesAsync(bool isTran = true)
        {
            return await _sqlSugarClient.SaveQueuesAsync(isTran);
        }

    }
}
